package com.atguigu.edu.service.impl;

import com.atguigu.edu.entity.vo.TeacherQuery;
import com.atguigu.edu.entity.Teacher;
import com.atguigu.edu.mapper.TeacherMapper;
import com.atguigu.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author ld
 * @since 2022-08-10
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public Page pageQuery(Long current, Long size, TeacherQuery teacherQuery) {
        Page<Teacher> pageInfo=new Page<>(current,size);
        LambdaQueryWrapper<Teacher> queryWrapper=new LambdaQueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        queryWrapper.like(!StringUtils.isEmpty(name),Teacher::getName,name)
                .eq(!StringUtils.isEmpty(level),Teacher::getLevel,level)
                .ge(!StringUtils.isEmpty(begin),Teacher::getGmtCreate,begin)
                .le(!StringUtils.isEmpty(end), Teacher::getGmtModified,end)
                .orderByDesc(Teacher::getGmtCreate)
                .orderByAsc(Teacher::getSort);
        page(pageInfo,queryWrapper);
        return pageInfo;
    }
    //得到前台首页teacher数据
    @Cacheable(value = "teacher",key = "'getIndexTeachers'")
    @Override
    public List<Teacher> getIndexTeacher() {
        LambdaQueryWrapper<Teacher> queryWrapperTeacher=new LambdaQueryWrapper<>();
        queryWrapperTeacher.orderByDesc(Teacher::getId)
                .last("limit 4");
        List<Teacher>  teacherList = baseMapper.selectList(queryWrapperTeacher);
        return teacherList;
    }
    //前台的分页查询
    @Override
    public Map<String, Object> pageInfo(long page,long limit) {
        Page<Teacher> pageInfo=new Page<>(page,limit);
        LambdaQueryWrapper<Teacher> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Teacher::getId);
        baseMapper.selectPage(pageInfo,queryWrapper);
        Map<String,Object> map=new HashMap<>();
        List<Teacher> records = pageInfo.getRecords();
        long current = pageInfo.getCurrent();
        long size = pageInfo.getSize();
        long pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        boolean hasPrevious = pageInfo.hasPrevious();
        boolean hasNext = pageInfo.hasNext();
        map.put("records",records);
        map.put("current",current);
        map.put("size",size);
        map.put("pages",pages);
        map.put("total",total);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        return map;
    }
}
